<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;
use App\Http\Requests\UserInsertRequest;
use Markdown;
use App\citys;
use Cart;
// use App\Http\Controllers\bianli;


class userControllers extends Controller
{
   
    public function index()
    {
        return view('index.index');
    }

    public function lu(Request $request)
    {
        // echo 'aaa';
        $username = $request->username;
        $user = User::where('username',$username)->first();
        if(empty($user)){
            return back()->with('info','账号不存在');
        }
        if(Hash::check($request->password,$user->password)) {
            session(['id'=>$user->id]);
            return redirect('/qq')->with('info','登陆成功');
            // echo '22';
        } else {
            return back();
            // echo 2222;
        }
    }
    public function zhuce()
    {
        return view('index.zhuce');
    }

    public function form(UserInsertRequest $request)
    {
        
        $user = new User;
        $user -> username = $request->username;
        $user -> password = Hash::make($request->input('password'));
        // dd($user->password);
        $user -> email = $request->email;
        if($user->save()) {
            return redirect('/deng')->with('info','注册成功！！！！');
        }else{
            return back();
        }
    }
    //图片
    public function tupian()
    {
        $imgs = User::paginate(3);
        return view('tupian',['touxiang'=>$imgs]);
    }
    //博客
    public function boke()
    {
        $boke = User::paginate(2);
        return view('boke',['boke'=>$boke]);
    }
    public function add()
    {   
        
        return view('add');
    }
    public function doadd(Request $request)
    {
        $boke = new User;
        $boke -> title = $request->input('title');
        $boke -> content = $request->input('content');
        if($boke->save()) {
            return redirect('/boke');
        } else {
            return back();
        }

    }
    public function yuedu($id)
    {
        $boke = User::find($id);
        return view('yuedu',['boke'=>$boke]);
    }
    public function xiugai($id)
    {
        $boke = User::find($id);
        return view('xiugai',['boke'=>$boke]);
    }
    public function doxiugai(Request $request,$id)
    {
        $boke = User::find($id);
        $boke -> title = $request->input('title');
        $boke -> content = $request->input('content');
        if($boke->save()) {
            return redirect('/boke');
        } else {
            return back();
        }
    }
    public function del($id)
    {
        $boke = User::where('id',$id)->delete();
        return redirect('/boke');
    }
    public function yulan(Request $request)
    {
        return Markdown::convertToHtml($request->content);
    }
    //无限遍历
    public function wuxian($arr,$id=0,$lv=0)
    {
        static $tree = [];
        foreach($arr as $v) {
            if($v['pid'] == $id){
                $v['lv'] = $lv;
                $tree[] = $v;
                $this -> wuxian($arr,$v['id'],$lv+1);
            }
        }
            return $tree;
        
    }
    public function bianli()
    {
        $citys = citys::get();
        $citys = $this -> wuxian($citys);
        return view('bianli',['citys'=>$citys]);
    }
    public function dobianli(Request $request)
    {
       $citys = new citys;
       $citys -> title = $request -> title;
       $citys -> pid = $request -> adminRole;
       $citys->save();
       return redirect('/bianli');
    }
    //购物车
    
    //添加到购物车
    public function addcart($id)
    {
        $products = [
            1=>['id'=>1,'name'=>'精灵女王','price'=>9.9,'des'=>'美丽妖艳'],
            2=>['id'=>2,'name'=>'梦幻熊猫','price'=>19.9,'des'=>'国宝'],
            3=>['id'=>3,'name'=>'风景','price'=>29.9,'des'=>'大雪纷飞'],
        ];
        $product = $products[$id];
        Cart::add($product['id'],$product['name'],1,$product['price']);
        return redirect()->route('cart');
    }
    //购物车列表
    public function list()
    {
        $carts = Cart::content();
        $subtotal = Cart::subtotal();
        $count = Cart::count();
        return view('liebiao',['carts'=>$carts,'subtotal'=>$subtotal,'count'=>$count]);

    }
    //删除购物车的商品
    public function dell($id)
    {
        Cart::remove($id);
        return redirect()->route('cart');

    }
    //清空购物车
    public function destroy()
    {
        Cart::destroy();
        return redirect()->route('cart');

    }

    //图片上传预览
    public function shangchuan()
    {
        return view('chuan');
    }
    public function doshangchuan(Request $request)
    {
        $prefix = "avatar".mt_rand(1,1000);
        $photo = $request->photo;
        $Origname = $photo->getClientOriginalName();
        $name = $prefix.$Origname;
        $src = "images/avatar/".$name;
        $realPath = $request->photo->getRealPath();
        Storage::disk('uploads')->put($src,file_get_contents($realPath));
    }
}


    